﻿// 2013年7月21日4:07:27
// 漠然
// 天龙 自动寻路系统

using System.Linq;
using Mrsoft.Model;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Mrsoft.Core {
    public static class AutoRouting {
        /// <summary>
        /// 静态构造函数
        /// 初始化MAP数据
        /// </summary>
        static AutoRouting() {
            Cache = new Dictionary<string, Dictionary<string, XY>>();
            XDocument xDoc = XDocument.Parse(Properties.Resources.map);
            //遍历 通过XML解析为节点信息
            var element = xDoc.Element("Config");
            if (element != null) {
                foreach (XElement xElement in element.Elements()) {
                    var temDic = new Dictionary<string, XY>();
                    foreach (var xElement1 in xElement.Elements()) {
                        var tarName = xElement1.Attribute("name").Value;
                        float x = float.Parse(xElement1.Attribute("x").Value);
                        float y = float.Parse(xElement1.Attribute("y").Value);
                        temDic.Add(tarName, new XY(x, y));
                    }
                    if (temDic.Count > 0)
                        Cache.Add(xElement.Name.LocalName, temDic);
                }
            }
            SplitNode = new HashSet<string> { "清源", "镜湖" };
        }

        /// <summary>
        /// 缓存传送点
        /// </summary>
        private static readonly Dictionary<string, Dictionary<string, XY>> Cache;

        /// <summary>
        /// 缓存地图
        /// </summary>
        private static readonly HashSet<string> SplitNode;

        /// <summary>
        /// 获取传送点
        /// </summary>
        /// <param name="srMap">当前地图</param>
        /// <param name="trMap">目标地图</param>
        /// <param name="transPoint">返回传送点</param>
        /// <returns>查询结构</returns>
        public static bool GetPoint(string srMap, string trMap, ref XY transPoint) {
            Dictionary<string, XY> temDic;
            if (Cache.TryGetValue(srMap, out temDic) && temDic.TryGetValue(trMap, out transPoint))
                return true;
            return false;
        }

        /// <summary>
        /// 获取寻路路径
        /// </summary>
        /// <param name="srMap"></param>
        /// <param name="trMap"></param>
        /// <param name="routes"></param>
        /// <param name="foreMap"></param>
        /// <returns></returns>
        public static bool GetRoute(string srMap, string trMap, List<string> routes, string foreMap = "") {
            if (string.IsNullOrEmpty(srMap) || string.IsNullOrEmpty(trMap))
                return false;
            if (string.IsNullOrEmpty(foreMap)) {
                routes.Clear();
                if (srMap == "梅岭" && trMap == "龙泉") {
                    routes.AddRange(new[] { "梅岭", "清源", "龙泉" });
                    return true;
                }
                if (trMap == "梅岭" && srMap == "龙泉") {
                    routes.AddRange(new[] { "龙泉", "清源", "梅岭" });
                    return true;
                }
            }
            Dictionary<string, XY> temDic;
            if (Cache.TryGetValue(srMap, out temDic)) {
                foreach (var tem in temDic) {
                    if (tem.Key == foreMap)
                        continue;
                    if ((tem.Key == trMap) || (!SplitNode.Contains(tem.Key) && GetRoute(tem.Key, trMap, routes, srMap))) {
                        routes.Insert(0, tem.Key);
                        if (foreMap == "") {
                            routes.Insert(0, srMap);
                        }
                        return true;
                    }
                }
            }
            return false;
        }



    }
}
